package com.stylefeng.guns.modular.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;

import com.stylefeng.guns.modular.system.jqgrid.CustomPage;
import com.stylefeng.guns.modular.system.jqgrid.ExcelUtil;
import com.stylefeng.guns.modular.system.jqgrid.ExportPage;
import com.stylefeng.guns.modular.system.jqgrid.FrontPage;
import com.stylefeng.guns.modular.system.model.ColModel;
import com.stylefeng.guns.modular.system.service.IColModelService;

/**
 * jqGrid列模型控制器
 *
 * @author Greedwind
 * @Date 2018-10-16 11:51:26
 */
@Controller
@RequestMapping("/colModel")
@SessionAttributes("genQo")
public class ColModelController extends BaseController {

    private String PREFIX = "/system/colModel/";

    @Autowired
    private IColModelService colModelService;

    /**
     * 跳转到jqGrid列模型首页
     */
    @RequestMapping("")
    public String index(Model model) {
		model.addAttribute("tablename", "");
        return PREFIX + "colModel.html";
    }

    /**
     * 跳转到添加jqGrid列模型
     */
    @RequestMapping("/colModel_add")
    public String colModelAdd() {
        return PREFIX + "colModel_add.html";
    }

    /**
     * 跳转到修改jqGrid列模型
     */
    @RequestMapping("/colModel_update/{colModelId}")
    public String colModelUpdate(@PathVariable Integer colModelId, Model model) {
        ColModel colModel = colModelService.selectById(colModelId);
        model.addAttribute("item",colModel);
        LogObjectHolder.me().set(colModel);
        return PREFIX + "colModel_edit.html";
    }
 
    /**
     * 获取ColModel列表(jqgrid)
     */
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list2(FrontPage<ColModel> page, HttpServletRequest request) {
		Page<ColModel> pageList = colModelService.selectPage(page.getPagePlus(),page.getWrapper());
		CustomPage<ColModel> customPage = new CustomPage<ColModel>(pageList);
		return customPage;
	}


    /**
     * 根据table_name获取ColModel
     */
	@RequestMapping(value="/index/{tableName}")
	public String index(@PathVariable String tableName, Model model) {
		model.addAttribute("tablename", tableName);
        return PREFIX + "colModel.html";
	}
	
	
    /**
   	 *导出Excel(jqgrid)
     */
	@ResponseBody
	@RequestMapping(value="/Export2Excel")
	public void Export2Excel(HttpServletRequest req,  HttpServletResponse res) {
		 String params = req.getParameter("export2excel_params");
		//json字串转POJO
		ExportPage<ColModel> page = JSON.parseObject(params, new TypeReference<ExportPage<ColModel>>(){});
		List<Map<String, Object>> list = colModelService.selectMaps(page.getWrapper());
        Map<String,String> map = new HashMap<String, String>();
		try {
			ExcelUtil<ColModel> export = new ExcelUtil<ColModel>();
			export.exportToExcel(res, list, page);
		} catch (Exception e) {
            e.printStackTrace();
		}
	}  	
	/**
     * 根据table_name获取ColModel列表(jqgrid)
     */
	@RequestMapping(value="/listbytblname/{tableName}")
	@ResponseBody
	public Object listByTblName(@PathVariable String tableName, FrontPage<ColModel> page, HttpServletRequest request) {
		Wrapper<ColModel> wrapper = page.getWrapper();
		wrapper.and().eq("table_name", tableName);
		Page<ColModel> pageList = colModelService.selectPage(page.getPagePlus(), wrapper);
		CustomPage<ColModel> customPage = new CustomPage<ColModel>(pageList);
		return customPage;
	}
    
    /**
     * 新增jqGrid列模型
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ColModel colModel) {
        colModelService.insert(colModel);
        return SUCCESS_TIP;
    }

    /**
     * 删除jqGrid列模型
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer colModelId) {
        colModelService.deleteById(colModelId);
        return SUCCESS_TIP;
    }

    /**
     * 修改jqGrid列模型
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ColModel colModel) {
        colModelService.updateById(colModel);
        return SUCCESS_TIP;
    }

    /**
     * jqGrid列模型详情
     */
    @RequestMapping(value = "/detail/{colModelId}")
    @ResponseBody
    public Object detail(@PathVariable("colModelId") Integer colModelId) {
        return colModelService.selectById(colModelId);
    }
    
    /*
    public List<ColModel> getColList(String tableName){
		Wrapper<ColModel> wrapper = new EntityWrapper<ColModel>();
		wrapper.and().eq("table_name", tableName);
		List<ColModel> list = colModelService.selectList(wrapper);
		return list;
    } 
    */   
}
